package com.example.exam.user.dao;

import com.example.exam.db.BaseDao;
import com.example.exam.db.PageInfoBean;
import com.example.exam.user.model.UserBean;
import org.apache.commons.lang3.StringUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.stereotype.Repository;

import java.text.SimpleDateFormat;
import java.util.*;

@Repository
public class UserDao extends BaseDao<UserBean, Integer> {
    @Override
        /**
            * @Description:
            * @Param: []
            * @return: java.lang.String
            * @Author: xhw
            * @Date: 2021/7/26
            */
    public String getAddSql() {
        return "insert into sys_user (id, show_name, login_name, password, salt, head,tel, create_time, status,login_time, create_user) values (:id, :show_name, :login_name, :password, :salt, :head,:tel, :create_time, :state, :login_time, :create_user) ";
    }
    @Override
        /**
            * @Description:
            * @Param: []
            * @return: java.lang.String
            * @Author: xhw
            * @Date: 2021/7/26
            */
    public String getRemoveByIdSql() {
        return "update sys_user set status = -1 where id = ? ";
    }
    @Override
        /**
            * @Description:
            * @Param: []
            * @return: java.lang.String
            * @Author: xhw
            * @Date: 2021/7/26
            */
    public String getUpdateAllFieldSql() {
        return "update sys_user set  show_name=:show_name,login_name=:login_name,tel=:tel where id=:id ";
    }
    /**
     * @Description:
     * @Param: [pageInfoBean, loginName, row, id]
     * @return: com.example.exam.db.PageInfoBean
     * @Author: xhw
     * @Date: 2021/7/26
     */public PageInfoBean list(PageInfoBean pageInfoBean, String loginName,Integer row,Integer id) {
         String querySql = "SELECT id,show_name,login_name,STATUS,create_time,type FROM sys_user WHERE STATUS=1";
        Map<String, Object> paramMap = new HashMap<String, Object>();
        if (StringUtils.isNotEmpty(loginName)){
            querySql += " AND login_name like :loginName";
            paramMap.put("loginName", "%" + loginName + "%");
        }
        if(row==0){
            querySql +=" AND create_user="+id;
        }
        //System.out.println("串"+JSON.toJSONString(pageForBean(querySql, paramMap, pageInfoBean)));
        return pageForBean(querySql, paramMap, pageInfoBean);
    }
    /**
     * @Description:
     * @Param: []
     * @return: java.util.List<com.example.exam.user.model.UserBean>
     * @Author: xhw
     * @Date: 2021/7/26
     */ public List<UserBean> listUser(){
         try{
            String sql="select id,login_name from sys_user WHERE STATUS=1 and type=1";
            return getJtl().query(sql,new BeanPropertyRowMapper<UserBean>(UserBean.class));
        }catch(Exception e){
            e.fillInStackTrace();
            return null;
        }
    }/**
     * @Description:
     * @Param: [username]
     * @return: com.example.exam.user.model.UserBean
     * @Author: xhw
     * @Date: 2021/7/26
     */
    public UserBean selectLogin(String username){

        try{
            String sql="SELECT id,PASSWORD,salt FROM sys_user WHERE (login_name=? OR tel=?) AND STATUS=1";
            List<Object> list=new ArrayList<Object>();
            list.add(username);
            list.add(username);
            //System.out.println(sql);
            UserBean ub=getJtl().queryForObject(sql,new BeanPropertyRowMapper<UserBean>(UserBean.class), list.toArray());
            return ub;
        }catch(Exception e){
            logger.error(e.fillInStackTrace());
            return null;
        }
    }
    @Override
        /**
            * @Description:
            * @Param: []
            * @return: java.lang.String
            * @Author: xhw
            * @Date: 2021/7/26
            */
    public String findByIdSql() {
        return "select * from sys_user where id = ?";
    }
    /**
     * @Description:
     * @Param: [login_time, id]
     * @return: java.lang.Integer
     * @Author: xhw
     * @Date: 2021/7/26
     */
    public Integer updateLoginTime(Date login_time,Integer id){
        SimpleDateFormat sim=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String loginTime= sim.format(login_time);
        String sql="UPDATE sys_user SET login_time=? WHERE id=?";
        Integer i=getJtl().update(sql,loginTime,id);
        //System.out.println("i是"+i);
        return i;
    }/**
     * @Description:
     * @Param: [id, type]
     * @return: java.lang.Integer
     * @Author: xhw
     * @Date: 2021/7/26
     */
    public Integer editType(Integer id,Integer type){
        String sql="UPDATE sys_user SET type=? WHERE id=?";
        List<Object> list=new ArrayList<Object>();
        list.add(type);
        list.add(id);
        return getJtl().update(sql,list.toArray());
    } /**
     * @Description:
     * @Param: [newPwd, salt, id]
     * @return: java.lang.Integer
     * @Author: xhw
     * @Date: 2021/7/26
     */
    public Integer editPwd(String newPwd,String salt,Integer id){
        String sql="UPDATE sys_user SET PASSWORD=?,salt=? WHERE id=?";
        List<Object> list=new ArrayList<Object>();
        list.add(newPwd);
        list.add(salt);
        list.add(id);
        return getJtl().update(sql,list.toArray());
    }/**
     * @Description:
     * @Param: [username]
     * @return: com.example.exam.user.model.UserBean
     * @Author: xhw
     * @Date: 2021/7/26
     */
    public UserBean selectId(String username){
        String sql="SELECT * FROM sys_user WHERE login_name=? or tel=?";
        List<Object> list=new ArrayList<Object>();
        list.add(username);
        list.add(username);
        return getJtl().queryForObject(sql,new BeanPropertyRowMapper<UserBean>(UserBean.class),list.toArray());
    }
}
